package org.fkjava.oa.identity.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.fkjava.common.dao.impl.BaseDaoImpl;
import org.fkjava.common.util.pager.PageModel;
import org.fkjava.oa.identity.dao.UserDao;
import org.fkjava.oa.identity.domain.Role;
import org.fkjava.oa.identity.domain.User;

/**
 * @author xlei
 * @Email dlei0006@163.com
 * @QQ 251425887
 * @Tel 18665616520
 * @Date 2017年1月14日上午9:44:47
 * @From http://www.fkjava.org 
 *
 */
public class UserDaoImpl extends BaseDaoImpl implements UserDao {

	@Override
	public List<User> selectUsersByPager(User user, PageModel pageModel) throws Exception {
		/** 
		 *  多条件查询   
		 * */
		StringBuilder hql = new StringBuilder();
		List<Object> params = new ArrayList<>();
		hql.append("select u from User u where 1=1 ");
		if(user!=null){
			/** 是否传入了姓名来查询  */
			if(!StringUtils.isEmpty(user.getName())){
				hql.append(" and u.name like ? ");
				params.add("%"+user.getName()+"%");
			}
			/** 是否传入手机号码了来查询  */
			if(!StringUtils.isEmpty(user.getPhone())){
				hql.append(" and u.phone like ? ");
				params.add("%"+user.getPhone()+"%");
			}
			/** 是否传入部门来查询  */
			if(user.getDept()!=null && user.getDept().getId()!=null && user.getDept().getId()!= 0 ){
				hql.append(" and u.dept.id = ? ");
				params.add(user.getDept().getId());
			}
			
			if(user.getJob()!=null && !StringUtils.isEmpty(user.getJob().getCode()) 
					&& !user.getJob().getCode().equals("0")){
				hql.append(" and u.job.code = ? ");
				params.add(user.getJob().getCode());
			}
		}
		return findByPage(hql.toString(), pageModel, params);
	}
	
	@Override
	public void deleteBulkUsers(String[] userIds) throws Exception {
		StringBuilder hql = new StringBuilder();
		hql.append("delete User u where u.userId in(");
		for(int i = 0 ; i < userIds.length ; i++ ){
			hql.append(i==userIds.length -1?"?)":"?,");
		}
		this.bulkUpdate(hql.toString(), userIds);
	}

	@Override
	public List<User> selectUserByRole(Role role, PageModel pageModel) throws Exception {
		List<Object> params = new ArrayList<>();
		params.add(role.getId());
		/** 查询属于这个角色下的所有用户 
		  *  查谁找谁 
		  * */
		String hql = "select u from User u inner join u.roles r where r.id = ? ";
		return findByPage(hql, pageModel, params);
		
	}

	@Override
	public List<User> selectUserByNotRole(Role role, PageModel pageModel) throws Exception {
		/** 
		 *  查谁找谁 
		 *  查询不属于某个角色下的用户: 子查询。
		 *  1.先查询出所有的用户 
		 *  2.再查询属于这个角色下的用户
		 *  3.再从所有用户中排除属于这个角色下的用户。
		 * */
		StringBuilder hql = new StringBuilder();
		hql.append(" select u from User u ");  // 1
		hql.append(" where u.userId not in( "); // 3
		hql.append(" select u.userId from User u inner join u.roles r where r.id = ? ") ;// 2
		hql.append(" ) ");
		List<Object> params = new ArrayList<>();
		params.add(role.getId());
		return findByPage(hql.toString(), pageModel, params);
	}
}







